package com.example.figurehecheng.service.impl;

import com.example.figurehecheng.domain.entity.Image;
import com.example.figurehecheng.mapper.ImageMapper;
import com.example.figurehecheng.service.IImageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.figurehecheng.domain.dto.ImageDTO;
import com.example.figurehecheng.domain.vo.ImageVO;
import java.time.LocalDateTime;

/**
 * <p>
 * 图片关联表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-04
 */
@Service
public class ImageServiceImpl extends ServiceImpl<ImageMapper, Image> implements IImageService {


    @Override
    public ImageVO saveImage(ImageDTO imageDTO) {
        System.out.println(imageDTO);
        Image image = new Image();
        image.setRelationId(imageDTO.getRelationId())
            .setType(imageDTO.getType())
            .setUrl(imageDTO.getUrl())
            .setSortOrder(imageDTO.getSortOrder())
            .setIsCover(imageDTO.getIsCover())
            .setCreatedAt(LocalDateTime.now());
            
        // 保存图片信息
        save(image);
        
        // 转换为VO对象
        ImageVO imageVO = new ImageVO();
        BeanUtils.copyProperties(image, imageVO);

        return imageVO;
    }

    @Override
    public boolean deleteImage(ImageDTO dto) {
        Long id = lambdaQuery().eq(Image::getUrl, dto.getUrl()).one().getId();
        return removeById(id);
    }
}
